from django.db import models
from decimal import Decimal


class MerchantBusinessCategory(models.Model):
    '''经营类目'''

    name = models.CharField(max_length=32, verbose_name='经营类目')

    class Meta:
        db_table = 'tb_merchant_business_category'
        verbose_name = '经营类目'
        verbose_name_plural = verbose_name


class Merchant(models.Model):
    '''权益商户明细'''

    SUBJECT_TYPE_CHOICES = (
        (1, '小微'),
        (2, '个体'),
        (3, '企业'),
        (4, '其他'),
        (5, '事业单位')
    )

    IS_ONLINE_CHOICES = (
        (1, '未上架'),
        (2, '已上架'),
        (3, '已下架')
    )

    SUBSIDY_CHANNEL_CHOICES = (
        (2, '瑞银信'),
        (3, '随行付')
    )

    IS_SUBSIDY_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_MALL_CHOICES = (
        (0, '不符合'),
        (1, '未上架'),
        (2, '已上架'),
        (3, '已下架'),
        (5, '审核中')
    )

    APPLTMENT_STATE_CHOICES = (
        (0, '-'),
        (1, '待审核'),
        (2, '已通过'),
        (3, '已驳回'),
        (4, '待签约'),
        (5, '待提交')
    )

    IS_PROFIT_SHARE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    name = models.CharField(verbose_name='商户名称', max_length=32)
    short_name = models.CharField(verbose_name='商户简称', max_length=32, null=True, blank=True)
    managerName = models.CharField(verbose_name='负责人姓名', max_length=32, null=True, blank=True)
    managermobile = models.CharField(verbose_name='负责人手机号码', max_length=11, null=True, blank=True)
    wyfMerchantID = models.CharField(verbose_name='微邮付商户号', max_length=16, null=True, blank=True)
    ruiyinxinMerchantID = models.CharField(verbose_name='湘邮商户号', max_length=32, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='入驻时间')
    subject_type = models.SmallIntegerField(verbose_name='主体类型', choices=SUBJECT_TYPE_CHOICES, default=1, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    account_name = models.CharField(verbose_name='开户名', max_length=128, null=True, blank=True)
    account_bank = models.CharField(verbose_name='开户行', max_length=128, null=True, blank=True)
    account_number = models.CharField(verbose_name='卡号', max_length=128, null=True, blank=True)
    identification_number = models.CharField(verbose_name='证件号码', max_length=32, null=True, blank=True)
    store_address = models.CharField(verbose_name='门店地址', max_length=128, null=True, blank=True)
    branch_num = models.IntegerField(verbose_name='入驻网点数量', default=0, blank=True)
    order_num = models.IntegerField(verbose_name='下单数量', default=0, blank=True)
    settlement_amount = models.DecimalField(verbose_name='结算金额', max_digits=10, decimal_places=2, default=0)
    store_header_copy = models.ImageField(verbose_name='门店门头照片', null=True, blank=True)
    is_online = models.SmallIntegerField(verbose_name='商户状态', choices=IS_ONLINE_CHOICES, default=1, blank=True)
    card_copy = models.ImageField(verbose_name='银行卡', null=True, blank=True)
    channel = models.ForeignKey('activities.Channel', verbose_name='所属通道', on_delete=models.SET_NULL, null=True, blank=True)
    subsidy_channel_id = models.SmallIntegerField(verbose_name='补贴通道', choices=SUBSIDY_CHANNEL_CHOICES, default=3)
    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='所属用户', on_delete=models.SET_NULL, null=True, blank=True)
    store_logo_copy = models.ImageField(verbose_name='门店门头照片', null=True, blank=True)
    open_work_time = models.DateTimeField(verbose_name='开始营业时间', null=True, blank=True)
    close_work_time = models.DateTimeField(verbose_name='结束营业时间', null=True, blank=True)
    business_category = models.ForeignKey('merchants.MerchantBusinessCategory', verbose_name='经营类目', on_delete=models.SET_NULL, null=True, blank=True)
    merchant_desc = models.CharField(verbose_name='简介', max_length=100, null=True, blank=True)
    prov_name = models.CharField(verbose_name='省', null=True, blank=True, max_length=16)
    city_name = models.CharField(verbose_name='市', null=True, blank=True, max_length=16)
    district_name = models.CharField(verbose_name='县', null=True, blank=True, max_length=16)
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)
    prov_code = models.CharField(verbose_name='省', null=True, blank=True, max_length=16)
    city_code = models.CharField(verbose_name='市', null=True, blank=True, max_length=16)
    district_code = models.CharField(verbose_name='县', null=True, blank=True, max_length=16)
    merchant_home_url = models.CharField(verbose_name='商户主页', null=True, blank=True, max_length=128)
    merchant_home_qrcode = models.ImageField(verbose_name='商户主页', null=True, blank=True)
    settlement_amount_by_day = models.DecimalField(verbose_name='结算金额', max_digits=10, decimal_places=2, default=8000)
    is_ryx_subsidy = models.SmallIntegerField(verbose_name='是否瑞银信开通补贴', choices=IS_SUBSIDY_CHOICES, default=0)
    is_sxf_subsidy = models.SmallIntegerField(verbose_name='是否瑞银信开通补贴', choices=IS_SUBSIDY_CHOICES, default=0)
    is_mall = models.SmallIntegerField(verbose_name='是否入驻商城', choices=IS_MALL_CHOICES, default=0)
    contract_table = models.ImageField(verbose_name='签约文件', null=True, blank=True)
    applyment_state = models.SmallIntegerField(verbose_name='分账开通状态', choices=APPLTMENT_STATE_CHOICES, default=0)
    verify_state = models.SmallIntegerField(verbose_name='客户经理确认状态', choices=APPLTMENT_STATE_CHOICES, default=0)
    is_profit_share = models.SmallIntegerField(verbose_name='是否分账', choices=IS_PROFIT_SHARE_CHOICES, null=True, blank=True)
    area = models.ForeignKey('goods.GoodsAreaCode', verbose_name='省市区', on_delete=models.SET_NULL, null=True, blank=True)
    device = models.CharField(verbose_name='设备id', max_length=16, null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant'
        verbose_name = '权益商户明细'
        verbose_name_plural = verbose_name


class MerchantMallApplyment(models.Model):
    '''商户入驻商城申请单'''

    APPLTMENT_STATE_CHOICES = (
        (0, '-'),
        (1, '待审核'),
        (2, '已通过'),
        (3, '已驳回'),
        (4, '待签约'),
        (5, '待提交')
    )

    APPLYMENT_TYPE_CHOICES = (
        (1, '入驻申请单'),
        (2, '审核申请单')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.CASCADE)
    applyment_state = models.SmallIntegerField(verbose_name='分账开通状态', choices=APPLTMENT_STATE_CHOICES, default=1)
    verify_state = models.SmallIntegerField(verbose_name='客户经理确认状态', choices=APPLTMENT_STATE_CHOICES, default=0)
    applyment_type = models.SmallIntegerField(verbose_name='状态', choices=APPLYMENT_TYPE_CHOICES, default=1)
    create_time = models.DateTimeField(verbose_name='提交时间', auto_now_add=True)
    audit_time = models.DateTimeField(verbose_name='提交时间', null=True, blank=True)
    context = models.CharField(verbose_name='入驻说明', max_length=128, null=True, blank=True)
    reject_reason = models.CharField(verbose_name='驳回原因', max_length=1024, null=True, blank=True)
    signature_copy = models.ImageField(verbose_name='手写签名', null=True, blank=True)
    contract = models.ImageField(verbose_name='签约文件', null=True, blank=True)
    verify_user= models.ForeignKey('users.User', verbose_name='确认者', on_delete=models.SET_NULL, null=True, blank=True)
    verify_time = models.DateTimeField(verbose_name='确认时间', null=True, blank=True)
    sign_url = models.ImageField(verbose_name='签约二维码', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_mall_apply'
        verbose_name = '商户入驻商城申请单'
        verbose_name_plural = verbose_name


class AreaCode(models.Model):
    """省市区编码"""

    code = models.CharField(verbose_name='编码', max_length=10, help_text='编码')
    province = models.CharField(verbose_name='省', max_length=20, help_text='省')
    city = models.CharField(verbose_name='市', max_length=20, help_text='市', null=True, blank=True)
    district = models.CharField(verbose_name='区', max_length=20, help_text='区', null=True, blank=True)
    province_code = models.CharField(verbose_name='编码', max_length=10, null=True, blank=True)

    class Meta:
        db_table = 'tb_area_code'
        verbose_name = '省市区编码'
        verbose_name_plural = verbose_name


class MerchantImage(models.Model):
    """merchant图片"""

    merchant = models.ForeignKey(Merchant, on_delete=models.CASCADE, verbose_name='merchant')
    image = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = 'tb_merchant_image'
        verbose_name = 'merchant图片'
        verbose_name_plural = verbose_name


class MerchantBill(models.Model):
    '''商户记账'''

    BILL_STATE_CHOICES = (
        (1, '待结算'),
        (2, '结算中'),
        (3, '已结算'),
        (4, '已撤销'),
        (5, '结算失败')
    )

    BILL_TYPE_CHOICES = (
        (1, '邮米账单'),
        (2, '商圈账单'),
        (3, '小邮美宿')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户通道信息', on_delete=models.CASCADE)
    receipts_time = models.DateTimeField(verbose_name='记账时间')
    point = models.DecimalField(verbose_name='进账邮米', max_digits=10, decimal_places=0, default=0)
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='进账金额', default=0)
    commission = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='手续费', default=0)
    payable_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='应付金额', default=0)
    bill_state = models.SmallIntegerField(verbose_name='账单状态', choices=BILL_STATE_CHOICES, default=1, blank=True)
    bill_type = models.SmallIntegerField(verbose_name='账单类型', choices=BILL_TYPE_CHOICES, default=1, blank=True)
    context = models.CharField(verbose_name='付款说明', max_length=128, null=True, blank=True)
    shop_order_id = models.CharField(verbose_name='订单号', max_length=32, null=True, blank=True)
    order_id = models.CharField(verbose_name='结算订单号', max_length=32, null=True, blank=True)
    settlement_time = models.DateTimeField(verbose_name='结算时间', null=True, blank=True)
    account_name = models.CharField(verbose_name='开户名', max_length=128, null=True, blank=True)
    account_bank = models.CharField(verbose_name='开户行', max_length=128, null=True, blank=True)
    account_number = models.CharField(verbose_name='卡号', max_length=128, null=True, blank=True)
    settlement_record = models.ForeignKey('merchants.MerchantSettlementRecord', verbose_name='结算记录', on_delete=models.SET_NULL, null=True, blank=True)
    channel = models.ForeignKey('activities.Channel', verbose_name='所属通道', on_delete=models.SET_NULL, null=True, blank=True)
    settlement_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='结算金额', default=0)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    other_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='其他交易金额', null=True, blank=True)
    other_commission = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='其他交易金额', null=True, blank=True)
    other_settlement_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='其他交易金额', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_bill'
        verbose_name = '商户记账'
        verbose_name_plural = verbose_name


class MerchantMonthlyBill(models.Model):
    '''商户月度账单'''

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户通道信息', on_delete=models.CASCADE)
    receipts_time = models.DateTimeField(verbose_name='记账时间')
    bill_name = models.CharField(verbose_name='账单名称', max_length=32)
    bill_time = models.CharField(verbose_name='账单时间', max_length=32)
    order_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单金额', default=0)
    order_count = models.IntegerField(verbose_name='订单数量', default=0)
    separate_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='分账金额', default=0)
    restitution_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='分账返还金额', default=0)
    settlement_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='结算金额', default=0)
    bill_file = models.ImageField(verbose_name='账单文件', null=True, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_monthly_bill'
        verbose_name = '商户月度账单'
        verbose_name_plural = verbose_name


class MerchantBillSettlementRecord(models.Model):
    '''结算关联记录'''

    settlement_record = models.ForeignKey('merchants.MerchantSettlementRecord', verbose_name='结算记录', on_delete=models.CASCADE)
    bill = models.ForeignKey('merchants.MerchantBill', verbose_name='商户记账', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='时间', auto_now_add=True)

    class Meta:
        db_table = 'tb_merchant_bill_settlement_record'
        verbose_name = '结算关联记录'
        verbose_name_plural = verbose_name


class MerchantSettlementRecord(models.Model):
    '''商户结算记录'''

    SETTLE_STATUS_CHOICES = (
        (3, '已结算'),
        (2, '结算中'),
        (5, '结算失败')
    )

    SETTLE_TYPE_CHOICES = (
        (1, '补贴出款'),
        (2, '退汇补出款')
    )

    IS_BIND_CHOICES = (
        (0, '未匹配'),
        (1, '已匹配')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户通道信息', on_delete=models.CASCADE)
    account_name = models.CharField(verbose_name='开户名', max_length=128, null=True, blank=True)
    account_bank = models.CharField(verbose_name='开户行', max_length=128, null=True, blank=True)
    account_number = models.CharField(verbose_name='卡号', max_length=128, null=True, blank=True)
    status = models.SmallIntegerField(verbose_name='账单状态', choices=SETTLE_STATUS_CHOICES, default=1, blank=True)
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='结算金额', default=0)
    settlement_time = models.DateTimeField(verbose_name='结算时间', null=True, blank=True)
    fail_reason = models.CharField(verbose_name='失败原因', max_length=128, null=True, blank=True)
    settle_type = models.SmallIntegerField(verbose_name='结算类型', choices=SETTLE_TYPE_CHOICES, default=1, blank=True)
    channel = models.ForeignKey('activities.Channel', verbose_name='所属通道', on_delete=models.SET_NULL, null=True, blank=True)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    is_bind = models.SmallIntegerField(verbose_name='是否绑定', choices=IS_BIND_CHOICES, default=0, blank=True)

    class Meta:
        db_table = 'tb_merchant_settlement_record'
        verbose_name = '商户结算记录'
        verbose_name_plural = verbose_name


class MerchantChannel(models.Model):
    '''商户通道信息'''

    IS_SUBSIDY_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户通道信息', on_delete=models.CASCADE)
    smid= models.CharField(verbose_name='支付宝商户号', max_length=32, null=True, blank=True)
    sub_mchid= models.CharField(verbose_name='微信间连商户号', max_length=32, null=True, blank=True)
    channel = models.ForeignKey('activities.Channel', verbose_name='所属通道', on_delete=models.CASCADE)
    channel_mchid = models.CharField(verbose_name='渠道id', max_length=32, null=True, blank=True)
    is_subsidy = models.SmallIntegerField(verbose_name='是否开通补贴', choices=IS_SUBSIDY_CHOICES, default=0)

    class Meta:
        db_table = 'tb_merchant_channel'
        verbose_name = '权益商户明细'
        verbose_name_plural = verbose_name


class MerchantEnter(models.Model):
    '''商户入驻网点关联表'''

    IS_ONLINE_CHOICES = (
        (1, '未上架'),
        (2, '已上架'),
        (3, '已下架')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='所属商户', on_delete=models.CASCADE)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    user = models.ForeignKey('users.User', verbose_name='邀请客户经理', on_delete=models.SET_NULL, null=True, blank=True)
    OrgId = models.CharField(verbose_name='所属机构', max_length=32, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='入驻时间')
    managerName = models.CharField(verbose_name='负责人姓名', max_length=32, null=True, blank=True)
    managermobile = models.CharField(verbose_name='负责人手机号码', max_length=11, null=True, blank=True)
    is_online = models.SmallIntegerField(verbose_name='商户状态', choices=IS_ONLINE_CHOICES, default=2, blank=True)

    class Meta:
        db_table = 'tb_merchant_enter'
        verbose_name = '商户入驻网点关联表'
        verbose_name_plural = verbose_name


class MerchantUser(models.Model):
    """自定义用户模型类"""

    AUTH_STATE_CHOICES = (
        (1, '未提交'),
        (2, '待审核'),
        (3, '已驳回'),
        (4, '已认证')
    )

    IS_PSBC_CARD_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    mobile = models.CharField(verbose_name='电话', max_length=11, unique=True)
    openid = models.CharField(verbose_name='用户openid', max_length=64, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='入驻时间', auto_now_add=True)
    avatarUrl = models.CharField(verbose_name='头像', max_length=512, null=True, blank=True)
    city = models.CharField(verbose_name='城市', max_length=32, null=True, blank=True)
    country = models.CharField(verbose_name='国家', max_length=32, null=True, blank=True)
    gender = models.IntegerField(verbose_name='国家', null=True, blank=True)
    nickName = models.CharField(verbose_name='昵称', max_length=32, null=True, blank=True)
    province = models.CharField(verbose_name='省份', max_length=32, null=True, blank=True)
    identification_name = models.CharField(verbose_name='证件姓名', max_length=32, null=True, blank=True)
    identification_number = models.CharField(verbose_name='证件号码', max_length=32, null=True, blank=True)
    identification_front_copy = models.ImageField(verbose_name='证件正面照片', null=True, blank=True)
    auth_state = models.SmallIntegerField(verbose_name='领取状态', choices=AUTH_STATE_CHOICES, default=1)
    is_psbc_card = models.SmallIntegerField(verbose_name='是否绑定邮储卡', choices=IS_PSBC_CARD_CHOICES, default=0)
    lucky_bag = models.DecimalField(max_digits=10, decimal_places=0, verbose_name='福袋', default=0)
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_m_merchant_user'
        verbose_name = '自定义用户模型类'
        verbose_name_plural = verbose_name


class WaivePaymentActivity(models.Model):
    '''免单活动'''

    ACTIVITY_STATE_CHOICES = (
        (1, '未开始'),
        (2, '进行中'),
        (3, '已暂停'),
        (4, '已结束')
    )

    IS_REPEAT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_KEGUAN_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    activity_name = models.CharField(verbose_name='活动名称', max_length=64)
    available_begin_time = models.DateTimeField(verbose_name='开始时间')
    available_end_time = models.DateTimeField(verbose_name='结束时间')
    desc = models.CharField(verbose_name='备注', max_length=100)
    activity_state = models.SmallIntegerField(verbose_name='活动状态', choices=ACTIVITY_STATE_CHOICES, default=1, blank=True)
    description = models.TextField(verbose_name='活动使用说明', null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    is_repeat = models.SmallIntegerField(verbose_name='是否开启重复校验', choices=IS_REPEAT_CHOICES, default=1, blank=True)
    receive_amount = models.DecimalField(verbose_name='金额', max_digits=10, decimal_places=2, default=0)
    join_number = models.IntegerField(verbose_name='参与数', default=0)
    is_keguan = models.SmallIntegerField(verbose_name='是否推送客管系统', choices=IS_KEGUAN_CHOICES, default=0)
    pushEventIds = models.CharField(verbose_name='营销事件ID', max_length=256, null=True, blank=True)

    class Meta:
        db_table = 'tb_waive_payment_activity'
        verbose_name = '免单活动'
        verbose_name_plural = verbose_name


class WaivePaymentAdvance(models.Model):
    '''推进历史'''

    ACTIVITY_STATE_CHOICES = (
        (1, '未开始'),
        (2, '进行中'),
        (3, '已暂停'),
        (4, '已结束')
    )

    PAYMENT_STATE_CHOICES = (
        (0, '未激活'),
        (1, '已激活'),
    )

    OPERATE_TYPE_CHOICES = (
        (1, '操作'),
        (2, '文件')
    )

    waivepaymentactivity = models.ForeignKey('merchants.WaivePaymentActivity', verbose_name='活动', on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name='推进时间', auto_now_add=True)
    activity_state = models.SmallIntegerField(verbose_name='活动状态', choices=ACTIVITY_STATE_CHOICES, default=1, blank=True)
    remark = models.TextField(verbose_name='说明', max_length=100, null=True, blank=True)
    file = models.FileField(verbose_name='文件', null=True, blank=True)
    create_user = models.ForeignKey('users.User', verbose_name='上传人', on_delete=models.PROTECT)
    payment_state = models.SmallIntegerField(verbose_name='领取状态', choices=PAYMENT_STATE_CHOICES, default=0, blank=True)
    trade_time_begin = models.DateTimeField(verbose_name='交易时(起)', null=True, blank=True)
    trade_time_end = models.DateTimeField(verbose_name='交易时(止)', null=True, blank=True)
    return_msg = models.TextField(verbose_name='失败原因', null=True, blank=True)
    operate_type = models.SmallIntegerField(verbose_name='领取状态', choices=OPERATE_TYPE_CHOICES, default=1, blank=True)
    name = models.CharField(verbose_name='备注', max_length=100, null=True, blank=True)
    activate_user = models.ForeignKey('users.User', verbose_name='激活操作人', on_delete=models.SET_NULL, null=True, blank=True, related_name='activate_user_set')
    activate_time = models.DateTimeField(verbose_name='激活时间', null=True, blank=True)
    join_number = models.IntegerField(verbose_name='参与数', default=0)

    class Meta:
        db_table = "tb_waive_payment_advance"
        verbose_name = '推进历史'
        verbose_name_plural = verbose_name


class WaivePaymentMerchant(models.Model):
    '''参与免单商户'''

    PAYMENT_TYPE_CHOICES = (
        (0, '谢谢参与'),
        (1, '中奖状态')
    )

    PAYMENT_STATE_CHOICES = (
        (0, '未激活'),
        (1, '待领取'),
        (2, '已领取'),
        (3, '领取失败'),
        (4, '已撤销'),
        (5, '待抽奖'),
        (6, '无效'),
        (7, '谢谢参与')
    )

    waivepaymentactivity = models.ForeignKey('merchants.WaivePaymentActivity', verbose_name='活动', on_delete=models.PROTECT)
    waivepaymentadvance = models.ForeignKey('merchants.WaivePaymentAdvance', verbose_name='历史id', on_delete=models.SET_NULL, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    business_no = models.CharField(verbose_name='商户号', max_length=16)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.SET_NULL, null=True, blank=True)
    payment_state = models.SmallIntegerField(verbose_name='领取状态', choices=PAYMENT_STATE_CHOICES, default=0, blank=True)
    payment_type = models.SmallIntegerField(verbose_name='领取状态', choices=PAYMENT_TYPE_CHOICES, default=1, blank=True)
    company_mobile = models.CharField(verbose_name='客户经理手机号', max_length=11, null=True, blank=True)
    received_time = models.DateTimeField(verbose_name='抽奖时间', null=True, blank=True)
    payment_no = models.CharField(verbose_name='支付订单号', max_length=64, null=True, blank=True)
    nonce_str = models.CharField(verbose_name='随机字符串', max_length=32)
    return_msg = models.TextField(verbose_name='失败原因', null=True, blank=True)
    partner_trade_no = models.CharField(verbose_name='商户订单号', max_length=32, null=True, blank=True)
    account = models.CharField(verbose_name='参与方的唯一标识', max_length=64, null=True, blank=True)
    name = models.CharField(verbose_name='参与方的唯一标识', max_length=128, null=True, blank=True)
    payment_time = models.DateTimeField(verbose_name='领取时间', null=True, blank=True)
    merchant_user = models.ForeignKey('merchants.MerchantUser', verbose_name='领取用户', on_delete=models.SET_NULL, null=True, blank=True)
    amount = models.DecimalField(verbose_name='金额', max_digits=10, decimal_places=2, default=0)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    periods = models.CharField(verbose_name='期数', max_length=8, null=True, blank=True)

    class Meta:
        db_table = 'tb_waive_payment_merchant'
        verbose_name = '参与免单商户'
        verbose_name_plural = verbose_name


class WaivePaymentOrder(models.Model):
    '''免单订单'''

    TRADE_TYPE_CHOICES = (
        (1, '支付宝'),
        (2, '微信'),
        (3, '云闪付'),
        (4, '本对本')
    )

    IS_HIT_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    waivepaymentactivity = models.ForeignKey('merchants.WaivePaymentActivity', verbose_name='活动', on_delete=models.PROTECT)
    waivepaymentmerchant = models.ForeignKey('merchants.WaivePaymentMerchant', verbose_name='活动商户', on_delete=models.PROTECT)
    order_no = models.CharField(verbose_name='交易单号', max_length=64)
    trade_no = models.CharField(verbose_name='交易凭证号', max_length=64, null=True, blank=True)
    trade_type = models.SmallIntegerField(verbose_name='交易类型', choices=TRADE_TYPE_CHOICES)
    rate_money = models.DecimalField(verbose_name='手续费', max_digits=10, decimal_places=2)
    trade_money = models.DecimalField(verbose_name='交易金额', max_digits=10, decimal_places=2)
    settlement_money = models.DecimalField(verbose_name='清分金额', max_digits=10, decimal_places=2)
    trade_time_begin = models.DateTimeField(verbose_name='交易时(起)')
    trade_time_end = models.DateTimeField(verbose_name='交易时(止)')
    channel = models.CharField(verbose_name='交易凭证号', max_length=32)
    is_hit = models.SmallIntegerField(verbose_name='是否免单', choices=IS_HIT_CHOICES, default=0, blank=True)

    class Meta:
        db_table = 'tb_waive_payment_order'
        verbose_name = '免单订单'
        verbose_name_plural = verbose_name


class WaivePaymentMerchantKeguan(models.Model):
    '''推送客管系统数据'''

    PUST_STATUS_CHOICES = (
        (1, '成功'),
        (2, '失败')
    )

    payment = models.OneToOneField('merchants.WaivePaymentMerchant', verbose_name='派发数据', on_delete=models.CASCADE)
    push_status = models.SmallIntegerField(verbose_name='推送状态', choices=PUST_STATUS_CHOICES, default=1)
    push_time = models.DateTimeField(verbose_name='推送时间')
    return_msg = models.TextField(null=True, blank=True)

    class Meta:
        db_table = 'tb_waive_payment_merchant_keguan'
        verbose_name = '推送客管系统数据'
        verbose_name_plural = verbose_name


class WaivePaymentActivityKeguanRecords(models.Model):
    '''推送客管系统数据操作记录'''

    OPERATE_TYPE_CHOICES = (
        (1, '开启'),
        (2, '关闭')
    )

    activity = models.ForeignKey('merchants.WaivePaymentActivity', verbose_name='活动', on_delete=models.CASCADE)
    operate_time = models.DateTimeField(verbose_name='操作时间', auto_now_add=True)
    operate_user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.CASCADE)
    operate_type = models.SmallIntegerField(verbose_name='操作类型', choices=OPERATE_TYPE_CHOICES, default=1)

    class Meta:
        db_table = 'tb_waive_payment_activity_keguan_records'
        verbose_name = '推送客管系统数据操作记录'
        verbose_name_plural = verbose_name


class MerchantBusinessQRCode(models.Model):
    '''商圈码'''

    IS_BIND_CHOICES = (
        (0, '未绑定'),
        (1, '已绑定')
    )

    IS_C_BIND_CHOICES = (
        (0, '未认证'),
        (1, '已认证')
    )

    IS_ACHIEVE_CHOICES = (
        (0, '未完成'),
        (1, '已完成')
    )

    AWARD_STATUS_CHOICES = (
        (0, '未领取'),
        (1, '已领取')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.SET_NULL, null=True, blank=True)
    bind_time = models.DateTimeField(verbose_name='绑定时间', null=True, blank=True)
    sn = models.CharField(verbose_name='SN码', max_length=12, unique=True)
    user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.SET_NULL, null=True, blank=True)
    is_bind = models.SmallIntegerField(verbose_name='是否绑定', choices=IS_BIND_CHOICES, default=0)
    is_c_bind = models.SmallIntegerField(verbose_name='是否绑定', choices=IS_C_BIND_CHOICES, default=0)
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='用户', on_delete=models.SET_NULL, null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='所属层级', on_delete=models.SET_NULL, null=True, blank=True)
    c_bind_time = models.DateTimeField(verbose_name='绑定时间', null=True, blank=True)
    inviter_count = models.IntegerField(verbose_name='邀请人数', default=0)
    inviter_total_count = models.IntegerField(verbose_name='总邀请人数', default=0)
    is_achieve = models.SmallIntegerField(verbose_name='任务状态', choices=IS_ACHIEVE_CHOICES, default=0)
    return_msg = models.TextField(verbose_name='失败说明', null=True, blank=True)
    cashactivitydata = models.OneToOneField('cashbonus.CashActivityData', verbose_name='奖品', on_delete=models.SET_NULL, null=True, blank=True)
    award_time = models.DateTimeField(verbose_name='完成任务时间', null=True, blank=True)
    award_status = models.SmallIntegerField(verbose_name='奖品状态', choices=AWARD_STATUS_CHOICES, default=0)
    sn_url = models.CharField(verbose_name='SN码', null=True, blank=True, max_length=128)
    sn_qrcode = models.ImageField(verbose_name='SN码', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_business_qrcode'
        verbose_name = '商圈码'
        verbose_name_plural = verbose_name


class MerchantBindCardOrder(models.Model):

    ORDER_STATUS_CHOICES = (
        (1, '待支付'),
        (2, '已支付'),
        (3, '支付失败'),
        (4, '已退款'),
        (5, '退款失败')
    )

    BANK_TYPE_CHOICES = (
        (1, '邮储借记卡'),
        (2, '邮储信用卡'),
        (3, '其他')
    )

    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='商户用户', on_delete=models.CASCADE)
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='支付金额', default=0)
    order_status = models.SmallIntegerField(verbose_name='订单状态', choices=ORDER_STATUS_CHOICES, default=1)
    bank_type_sign = models.SmallIntegerField(verbose_name='银行标识', choices=BANK_TYPE_CHOICES, default=3)
    description = models.CharField(verbose_name='商品描述', max_length=127)
    out_trade_no = models.CharField(verbose_name='商户订单号', max_length=32, unique=True)
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    user_received_account = models.CharField(verbose_name='退款入账账户', max_length=64, null=True, blank=True)
    refund_id = models.CharField(verbose_name='微信支付退款单号', max_length=32, null=True, blank=True)
    out_refund_no = models.CharField(verbose_name='商户退款单号', max_length=32, null=True, blank=True)
    transaction_id = models.CharField(verbose_name='交易单号', max_length=32)
    message = models.CharField(verbose_name='交易单号', max_length=128, null=True, blank=True)
    bank_type = models.CharField(verbose_name='付款银行类型', max_length=16, null=True, blank=True)
    success_time = models.DateTimeField(verbose_name='成功支付时间', null=True, blank=True)
    refund_time = models.DateTimeField(verbose_name='该笔交易的退款时间', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_bind_card_order'
        verbose_name = '绑卡校验订单'
        verbose_name_plural = verbose_name


class MerchantEightEightWhiteTable(models.Model):

    WIN_TYPE_CHOICES = (
        (1, '1.88'),
        (2, '2.88'),
        (3, '18.88'),
        (4, '88.88'),
        (5, '888.88'),
        (6, '谢谢参与')
    )

    AMOUNT_ENUM = {
        1: Decimal('3.88'),
        2: Decimal('8.88'),
        3: Decimal('18.88'),
        4: Decimal('88.88'),
        5: Decimal('888.88'),
        7: Decimal('38.88'),
        8: Decimal('188.88'),
        9: Decimal('13.88'),
        10: Decimal('388.88'),
        11: Decimal('10.88'),
        12: Decimal('1.88'),
    }

    IS_WIN_TYPE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_VISIBLE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    business_no = models.CharField(verbose_name='商户号', max_length=16)
    activity_id = models.IntegerField(verbose_name='所属活动', default=1)
    is_win_type = models.SmallIntegerField(verbose_name='是否指定中奖', choices=IS_WIN_TYPE_CHOICES, default=0)
    is_visible = models.SmallIntegerField(verbose_name='是否可见', choices=IS_VISIBLE_CHOICES, null=True, blank=True)
    win_type = models.SmallIntegerField(verbose_name='是否指定中奖', choices=WIN_TYPE_CHOICES, null=True, blank=True)
    expired_time = models.DateTimeField(verbose_name='过期时间', null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='所属层级', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_eigth_eight_white_table'
        verbose_name = '商户88节名单'
        verbose_name_plural = verbose_name


class MerchantEightEightDraw(models.Model):

    STATE_CHOICES = (
        (1, '待领取'),
        (2, '已领取'),
        (3, '领取失败'),
        (4, '已撤销'),
        (5, '领取中')
    )

    WIN_TYPE_CHOICES = (
        (1, '1.88'),
        (2, '2.88'),
        (3, '18.88'),
        (4, '88.88'),
        (5, '888.88'),
        (6, '谢谢参与')
    )

    AMOUNT_ENUM = {
        1: Decimal('3.88'),
        2: Decimal('8.88'),
        3: Decimal('18.88'),
        4: Decimal('88.88'),
        5: Decimal('888.88'),
        7: Decimal('38.88'),
        8: Decimal('188.88'),
        9: Decimal('13.88'),
        10: Decimal('388.88'),
        11: Decimal('10.88'),
        12: Decimal('1.88'),
    }

    amount = models.DecimalField(verbose_name='金额', max_digits=10, decimal_places=2, default=0)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    received_time = models.DateTimeField(verbose_name='领取时间', null=True, blank=True)
    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='领取用户', on_delete=models.CASCADE, null=True, blank=True)
    state = models.SmallIntegerField(verbose_name='领取状态', choices=STATE_CHOICES, default=1, blank=True)
    expired_time = models.DateTimeField(verbose_name='过期时间', null=True, blank=True)
    payment_no = models.CharField(verbose_name='支付订单号', max_length=64, null=True, blank=True)
    desc = models.CharField(verbose_name='备注', max_length=100)
    return_msg = models.TextField(null=True, blank=True)
    partner_trade_no = models.CharField(verbose_name='商户订单号', max_length=32, null=True, blank=True)
    payeeFullName = models.CharField(verbose_name='支付宝付款信息', max_length=32, null=True, blank=True)
    mobile = models.CharField(max_length=11, verbose_name='手机号', null=True, blank=True)
    win_type = models.SmallIntegerField(verbose_name='卡牌状态', choices=WIN_TYPE_CHOICES, default=4)
    business_no = models.CharField(verbose_name='商户号', max_length=16)
    login_id = models.CharField(verbose_name='支付宝账号', max_length=32, null=True, blank=True)
    realname = models.CharField(verbose_name='支付宝账号实名', max_length=32, null=True, blank=True)
    account = models.CharField(verbose_name='id', max_length=32)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    activity_id = models.IntegerField(verbose_name='所属活动', default=1)
    alidiscountcouponinfo_list = models.CharField(verbose_name='券id', max_length=128, null=True, blank=True)
    index = models.IntegerField(verbose_name='第几次抽奖', default=1)
    channel = models.ForeignKey('activities.Channel', verbose_name='派发渠道', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_eigth_eight_draw'
        verbose_name = '商户88节抽奖明细'
        verbose_name_plural = verbose_name


class MerchantEightEightActivity(models.Model):
    '''商户88节活动'''

    ACTIVITY_STATUS_CHOICES = (
        (1, '未开始'),
        (2, '进行中'),
        (3, '已结束')
    )

    ACTIVITY_TYPE_CHOICES = (
        (1, '商户节'),
        (2, '碰一下'),
        (3, '交易攒福礼'),
        (4, '认证达标'),
    )

    IS_KEGUAN_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    activity_name = models.CharField(verbose_name='活动名称', max_length=32, null=True, blank=True)
    activity_id = models.IntegerField(verbose_name='所属活动')
    activity_start_time = models.DateTimeField(verbose_name='开始时间')
    activity_end_time = models.DateTimeField(verbose_name='结束时间')
    draw_end_time = models.DateTimeField(verbose_name='抽奖结束时间', null=True, blank=True)
    index = models.IntegerField(verbose_name='所属活动')
    activity_description = models.TextField(verbose_name='活动名称', null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)
    activity_status = models.SmallIntegerField(verbose_name='活动状态', choices=ACTIVITY_STATUS_CHOICES, default=1)
    activity_type = models.SmallIntegerField(verbose_name='活动状态', choices=ACTIVITY_TYPE_CHOICES, default=1)
    total_amount = models.DecimalField(verbose_name='总金额', max_digits=10, decimal_places=2, default=0)
    total_count = models.IntegerField(verbose_name='总笔数', default=0)
    is_keguan = models.SmallIntegerField(verbose_name='是否推送客管系统', choices=IS_KEGUAN_CHOICES, default=0)
    pushEventIds = models.CharField(verbose_name='营销事件ID', max_length=256, null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_eight_eight_activity'
        verbose_name = '商户88节活动'
        verbose_name_plural = verbose_name


class MerchantEightEightDrawKeguan(models.Model):
    '''推送客管系统数据'''

    PUST_STATUS_CHOICES = (
        (1, '成功'),
        (2, '失败')
    )

    draw = models.OneToOneField('merchants.MerchantEightEightDraw', verbose_name='派发数据', on_delete=models.CASCADE)
    push_status = models.SmallIntegerField(verbose_name='推送状态', choices=PUST_STATUS_CHOICES, default=1)
    push_time = models.DateTimeField(verbose_name='推送时间')
    return_msg = models.TextField(null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_eigth_eight_draw_keguan'
        verbose_name = '推送客管系统数据'
        verbose_name_plural = verbose_name


class MerchantEightEightActivityKeguanRecords(models.Model):
    '''推送客管系统数据操作记录'''

    OPERATE_TYPE_CHOICES = (
        (1, '开启'),
        (2, '关闭')
    )

    activity = models.ForeignKey('merchants.MerchantEightEightActivity', verbose_name='活动', on_delete=models.CASCADE)
    operate_time = models.DateTimeField(verbose_name='操作时间', auto_now_add=True)
    operate_user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.CASCADE)
    operate_type = models.SmallIntegerField(verbose_name='操作类型', choices=OPERATE_TYPE_CHOICES, default=1)

    class Meta:
        db_table = 'tb_merchant_eight_eight_activity_keguan_records'
        verbose_name = '推送客管系统数据操作记录'
        verbose_name_plural = verbose_name


class MerchantEightEightActivityKeGuanLog(models.Model):
    #商家达标奖励-存量活动客管
    PUST_STATUS_CHOICES = (
        (1, '成功'),
        (2, '失败')
    )

    activity = models.OneToOneField('merchants.MerchantEightEightActivity', verbose_name='活动id', on_delete=models.CASCADE)
    push_status = models.SmallIntegerField(verbose_name='推送状态', choices=PUST_STATUS_CHOICES, default=1)
    push_time = models.DateTimeField(verbose_name='推送时间')
    return_msg = models.TextField(null=True, blank=True)
    class Meta:
        db_table = 'tb_merchant_eight_eight_activity_keguan_log'
        verbose_name = "商家达标奖励-存量活动客管记录"
        verbose_name_plural = verbose_name



class MerchantEightEightActivityOrder(models.Model):
    '''商户达标订单'''

    IS_STANDARD_CHOICES = (
        (0, '否'),
        (1, '是'),
        (2, '是')
    )

    trade_time = models.DateTimeField(verbose_name='交易日期')
    business_no = models.CharField(verbose_name='商户号', max_length=10)
    is_standard = models.SmallIntegerField(verbose_name='是否达标', choices=IS_STANDARD_CHOICES, default=0)
    trade_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='交易金额', default=0)
    activity = models.ForeignKey('merchants.MerchantEightEightActivity', verbose_name='活动', on_delete=models.CASCADE)
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = "tb_merchant_eight_eight_activity_order"
        verbose_name = '商户达标订单'
        verbose_name_plural = verbose_name


class MerchantEightEightActivityRelevance(models.Model):

    activity = models.ForeignKey('merchants.MerchantEightEightActivity', verbose_name='活动', on_delete=models.CASCADE)
    level = models.ForeignKey('users.Level', verbose_name='层级', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_eight_eight_activity_relevance'
        verbose_name = '商户88节活动'
        verbose_name_plural = verbose_name


class MerchantEightEightActivityPrize(models.Model):
    '''商户88节活动'''

    PRIZE_TYPE_CHOICES = (
        (1, '支付宝鼓励金'),
        (0, '谢谢参与'),
        (2, '支付宝权益券')
    )

    activity = models.ForeignKey('merchants.MerchantEightEightActivity', verbose_name='所属活动', on_delete=models.CASCADE)
    prize_type = models.SmallIntegerField(verbose_name='奖品类型', choices=PRIZE_TYPE_CHOICES, default=1)
    amount = models.DecimalField(verbose_name='金额', max_digits=10, decimal_places=2, default=0)
    prize_name = models.CharField(verbose_name='奖品名称', max_length=32)
    count = models.IntegerField(verbose_name='限制数量')
    alidiscountcoupon = models.ForeignKey('vouchers.AliDiscountCoupon', verbose_name='代金券活动', on_delete=models.SET_NULL, null=True, blank=True)
    prize_count = models.IntegerField(verbose_name='奖品数量', null=True, blank=True)
    name = models.CharField(verbose_name='奖品名称', max_length=32, null=True, blank=True)
    second_count = models.IntegerField(verbose_name='限制数量', default=0)

    class Meta:
        db_table = 'tb_merchant_eight_eight_activity_prize'
        verbose_name = '商户88节活动'
        verbose_name_plural = verbose_name


class MerchantBusinessQRCodeVisit(models.Model):
    '''商圈码访问'''

    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='领取用户', on_delete=models.CASCADE)
    qrcode = models.ForeignKey('merchants.MerchantBusinessQRCode', verbose_name='商圈码', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='访问时间', auto_now_add=True)

    class Meta:
        db_table = 'tb_merchant_business_qrcode_visit'
        verbose_name = '商圈码访问'
        verbose_name_plural = verbose_name


class MerchantBusinessQRCodeVisit1(models.Model):
    '''商圈码访问'''

    ip = models.CharField(verbose_name='访问用户ip', max_length=16)
    qrcode = models.ForeignKey('merchants.MerchantBusinessQRCode', verbose_name='商圈码', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='访问时间', auto_now_add=True)

    class Meta:
        db_table = 'tb_merchant_business_qrcode_visit_1'
        verbose_name = '商圈码访问'
        verbose_name_plural = verbose_name


class MerchantBillPushDate(models.Model):
    '''邮米补贴推送时间'''

    IS_PUSH_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    count = models.IntegerField(verbose_name='限制天数')
    push_date = models.CharField(verbose_name='推送日期', max_length=16, unique=True)
    is_push = models.SmallIntegerField(verbose_name='是否推送', choices=IS_PUSH_CHOICES)

    class Meta:
        db_table = 'tb_merchant_bill_push_date'
        verbose_name = '邮米补贴推送时间'
        verbose_name_plural = verbose_name


class MerchantBenBenOrder(models.Model):
    '''商户交易订单'''

    unionid  = models.CharField(verbose_name='平台unionid', max_length=32)
    wyf_openid = models.CharField(verbose_name='微邮付⼩程序openid', max_length=32)
    shop_id = models.CharField(verbose_name='商户id', max_length=32)
    money = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='支付金额', default=0)
    out_trade_no = models.CharField(verbose_name='微邮付订单号', max_length=32, unique=True)
    pay_time = models.DateTimeField(verbose_name='支付时间')
    transaction_id = models.CharField(verbose_name='付款凭证中的条码单号', max_length=64)
    channel_trade_no = models.CharField(verbose_name='微信⽀付/云闪付单号', max_length=64)

    class Meta:
        db_table = 'tb_merchant_benben_order'
        verbose_name = '本对本交易订单'
        verbose_name_plural = verbose_name


class MerchantBankQRCodeQuest(models.Model):
    '''商户绑卡信息上传记录'''

    FILE_TYPE_CHOICES = (
        (1, '商户明细'),
        (2, '绑卡明细'),
        (3, '绑卡统计')
    )

    FILE_STATUS_CHOICES = (
        (1, '等待中'),
        (2, '进行中'),
        (3, '已完成'),
        (4, '失败')
    )

    create_user = models.ForeignKey('users.User', verbose_name='上传者', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='上传时间', auto_now_add=True)
    file = models.FileField(verbose_name='文件', null=True, blank=True)
    logs_file = models.FileField(verbose_name='日志文件', null=True, blank=True)
    file_type = models.SmallIntegerField(verbose_name='文件类型', choices=FILE_TYPE_CHOICES)
    file_status = models.SmallIntegerField(verbose_name='任务状态', choices=FILE_STATUS_CHOICES)
    total_line = models.IntegerField(verbose_name='总行数', default=0)
    success_line = models.IntegerField(verbose_name='成功行数', default=0)
    error_line = models.IntegerField(verbose_name='失败行数', default=0)
    remark = models.TextField(verbose_name='录入说明', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_bank_qrcode_quest'
        verbose_name = '商户绑卡信息上传记录'
        verbose_name_plural = verbose_name


class MerchantBankQRCode(models.Model):
    '''商户银行码'''

    IS_DOWNLOAD_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    merchant = models.OneToOneField('merchants.Merchant', verbose_name='商户', on_delete=models.CASCADE)
    code_id = models.CharField(verbose_name='子码编码', max_length=32, unique=True)
    parent_code_id = models.CharField(verbose_name='父码编码', max_length=16, null=True, blank=True)
    link = models.CharField(verbose_name='员工码链接', max_length=256)
    qrcode = models.ImageField(verbose_name='二维码', null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='所属层级', on_delete=models.SET_NULL, null=True, blank=True)
    quest = models.ForeignKey('merchants.MerchantBankQRCodeQuest', verbose_name='任务', on_delete=models.SET_NULL, null=True, blank=True)
    business_no = models.CharField(verbose_name='商户号', max_length=16, null=True, blank=True)
    merchant_name = models.CharField(verbose_name='商户名称', max_length=256, null=True, blank=True)
    is_download = models.SmallIntegerField(verbose_name='是否下载', choices=IS_DOWNLOAD_CHOICES, default=0)
    visit = models.IntegerField(verbose_name='浏览次数', default=0)
    total_number = models.IntegerField(verbose_name='绑卡数', default=0)
    total_first_number = models.IntegerField(verbose_name='首绑数', default=0)

    class Meta:
        db_table = 'tb_merchant_bank_qrcode'
        verbose_name = '商户银行码'
        verbose_name_plural = verbose_name


class MerchantBankQRCodeBindData(models.Model):
    '''商户银行码绑卡明细'''

    CARD_TYPE_CHOICES = (
        (1, '储蓄卡'),
        (2, '信用卡')
    )

    IS_FIRST_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.CASCADE)
    code_id = models.CharField(verbose_name='子码编码', max_length=32)
    level = models.ForeignKey('users.Level', verbose_name='所属层级', on_delete=models.CASCADE)
    card_number = models.CharField(verbose_name='卡号', max_length=16)
    is_first = models.SmallIntegerField(verbose_name='是否首绑', choices=IS_FIRST_CHOICES)
    card_type = models.SmallIntegerField(verbose_name='卡类型', choices=CARD_TYPE_CHOICES)
    bind_time = models.DateTimeField(verbose_name='绑卡时间')
    bank_qrcode = models.ForeignKey('merchants.MerchantBankQRCode', verbose_name='所属银行码', on_delete=models.SET_NULL, null=True, blank=True)
    quest = models.ForeignKey('merchants.MerchantBankQRCodeQuest', verbose_name='任务', on_delete=models.SET_NULL, null=True, blank=True)
    business_no = models.CharField(verbose_name='商户号', max_length=16, null=True, blank=True)
    merchant_name = models.CharField(verbose_name='商户名称', max_length=256, null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_bank_qrcode_bind_data'
        verbose_name = '商户银行码绑卡明细'
        verbose_name_plural = verbose_name


class MErchantBankQPCodeStatistics(models.Model):

    bind_time = models.DateTimeField(verbose_name='绑卡时间')
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.CASCADE)
    code_id = models.CharField(verbose_name='子码编码', max_length=32)
    level = models.ForeignKey('users.Level', verbose_name='所属层级', on_delete=models.CASCADE)
    bank_qrcode = models.ForeignKey('merchants.MerchantBankQRCode', verbose_name='所属银行码', on_delete=models.SET_NULL, null=True, blank=True)
    quest = models.ForeignKey('merchants.MerchantBankQRCodeQuest', verbose_name='任务', on_delete=models.SET_NULL, null=True, blank=True)
    business_no = models.CharField(verbose_name='商户号', max_length=16, null=True, blank=True)
    merchant_name = models.CharField(verbose_name='商户名称', max_length=256, null=True, blank=True)
    total_number = models.IntegerField(verbose_name='绑卡数', default=0)
    total_first_number = models.IntegerField(verbose_name='首绑数', default=0)

    class Meta:
        db_table = 'tb_merchant_bank_qrcode_statistics'
        verbose_name = '商户银行码绑卡统计'
        verbose_name_plural = verbose_name


class XYMSMerchant(models.Model):
    '''小邮美宿商户'''

    IS_ACTIVE_CHOICES = (
        (0, '下架'),
        (1, '上架')
    )

    business_no = models.CharField(verbose_name='商户号', max_length=32)
    contentName = models.CharField(verbose_name='名称', max_length=128)
    contentImgList = models.TextField(verbose_name='主图')
    contentLabelList = models.TextField(verbose_name='标签列表')
    contentScore = models.CharField(verbose_name='评分，保留一位小数', max_length=8)
    contentPrice = models.CharField(verbose_name='价格', max_length=16)
    linkAppId = models.CharField(verbose_name='小程序appId', max_length=32)
    linkMpId = models.CharField(verbose_name='小程序原始Id', max_length=32)
    linkUrl = models.CharField(verbose_name='跳转链接', max_length=256)
    beginTime = models.CharField(verbose_name='营业时间-开始时间，格式-hh:mm', max_length=8)
    endTime = models.CharField(verbose_name='营业时间-结束时间，格式-hh:mm', max_length=8)
    contentAddress = models.CharField(verbose_name='地址,明文传递', max_length=256)
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)
    is_active = models.SmallIntegerField(verbose_name='状态', choices=IS_ACTIVE_CHOICES, default=1)
    merchant_type = models.TextField(verbose_name='商户分类', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_xiaoyoumeisu'
        verbose_name = '小邮美宿商户'
        verbose_name_plural = verbose_name


class XYMSMerchantImage(models.Model):
    '''小邮美宿图片'''

    merchant = models.ForeignKey('merchants.XYMSMerchant', verbose_name='商户', on_delete=models.SET_NULL, null=True, blank=True)
    image = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = 'tb_merchant_xiaoyoumeisu_image'
        verbose_name = '小邮美宿图片'
        verbose_name_plural = verbose_name


class MerchantEnterQuest(models.Model):
    '''商户入驻网点任务'''

    FILE_STATUS_CHOICES = (
        (1, '等待中'),
        (2, '进行中'),
        (3, '已完成'),
        (4, '失败')
    )

    create_user = models.ForeignKey('users.User', verbose_name='上传者', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='上传时间', auto_now_add=True)
    file = models.FileField(verbose_name='文件', null=True, blank=True)
    logs_file = models.FileField(verbose_name='日志文件', null=True, blank=True)
    file_status = models.SmallIntegerField(verbose_name='任务状态', choices=FILE_STATUS_CHOICES)
    total_line = models.IntegerField(verbose_name='总行数', default=0)
    success_line = models.IntegerField(verbose_name='成功行数', default=0)
    error_line = models.IntegerField(verbose_name='失败行数', default=0)
    remark = models.TextField(verbose_name='录入说明', null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_enter_quest'
        verbose_name = '商户入驻网点任务'
        verbose_name_plural = verbose_name


class MerchantNFCDevice(models.Model):
    '''商户NFC碰一下设备'''

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='所属商户', on_delete=models.SET_NULL, null=True, blank=True)
    sn = models.CharField(verbose_name='sn码', max_length=32, unique=True)

    class Meta:
        db_table = 'tb_merchant_nfc_device'
        verbose_name = '商户NFC碰一下设备'
        verbose_name_plural = verbose_name


class MerchantNFCDeviceTrade(models.Model):
    '''商户NFC碰一下交易数据'''

    PAY_TYPE_CHOICES = (
        (1, 'NFC'),
        (2, '扫码')
    )

    merchant = models.ForeignKey('merchants.Merchant', verbose_name='所属商户', on_delete=models.SET_NULL, null=True, blank=True)
    sn = models.CharField(verbose_name='sn码', max_length=32)
    device_type = models.CharField(verbose_name='设备类型', max_length=32)
    store_name = models.CharField(verbose_name='门店名称', max_length=64)
    province = models.CharField(verbose_name='省份', max_length=20)
    city = models.CharField(verbose_name='城市', max_length=20)
    district = models.CharField(verbose_name='县区', max_length=20)
    address = models.CharField(verbose_name='门店地址', max_length=128)
    transaction_id = models.CharField(verbose_name='交易号', max_length=64)
    trade_type = models.CharField(verbose_name='交易类型', max_length=16)
    total_amount = models.DecimalField(verbose_name='交易总金额', max_digits=10, decimal_places=2, default=0)
    trade_amount = models.DecimalField(verbose_name='交易金额', max_digits=10, decimal_places=2, default=0)
    trade_time = models.DateTimeField(verbose_name='交易时间')
    pay_type = models.SmallIntegerField(verbose_name='交易类型', choices=PAY_TYPE_CHOICES, null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='所属机构', on_delete=models.SET_NULL, null=True, blank=True)
    shop_id = models.CharField(verbose_name='商户id', max_length=32, null=True, blank=True)
    smid = models.CharField(verbose_name='支付宝账号', max_length=32, null=True, blank=True)
    device = models.ForeignKey('alipaynfc.NFCDevice', verbose_name='设备', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_merchant_nfc_device_trade'
        verbose_name = '商户NFC碰一下交易数据'
        verbose_name_plural = verbose_name

